<?php

class Subcategory {
	private $id;
	private $id_category;
	private $description;
	
	function __construct($id,$id_category,$description){
		$this -> id 			= $id;
		$this -> id_category 	= $id_category;
		$this -> description 	= $description;
	}
	
	function getSubcategory($db,$id){
		$result			= $db->query("SELECT * FROM subcategory WHERE sub_IDsubcategory = $id",2);
		$subcategory 	= new Subcategory(	
							$result[0]['sub_IDsubcategory'],
							$result[0]['sub_id_category'],
							$result[0]['sub_description']);
		return $subcategory;
	}
	
	function getList($db){
		$result=$db->query("SELECT *, cat_description FROM subcategory JOIN category ON cat_IDcategory = sub_id_category",2);
		return $result;
	}
	
	

	function insert($db,$subcategory){
		$id_category 	= $subcategory -> getIDcategory();
		$description 	= $subcategory -> getDescription();
		$result=$db->insert("INSERT INTO subcategory (sub_id_category,sub_description)
										 VALUES($id_category,'".addslashes(utf8_decode($description))."')");
		return $result;
	}

	function delete($db,$subcategory){
		$id 	= $subcategory -> getIDsubcategory();
		$result = $db -> delete("DELETE FROM subcategory WHERE sub_IDsubcategory = $id");
		return $result;
	}

	function update($db,$subcategory){
		$id 			= $subcategory -> getIDsubcategory();
		$id_category 	= $subcategory -> getIDcategory();
		$description 	= $subcategory -> getDescription();
		$result = $db -> update ("UPDATE subcategory SET sub_id_category 	= $id_category, 
														 sub_description 	= '".addslashes(utf8_decode($description))."'
													  WHERE sub_IDsubcategory = $id");
		return $result;
	}
	

	function getIDsubcategory(){
		return $this -> id;
	}	
	function setIDsubcategory($id){
		$this -> id = $id;
	}	

	function getIDcategory(){
		return $this -> id_category;
	}	
	function  setIDcategory($id_category){
		$this -> id_category = $id_category;
	}	

	function getDescription(){
		return $this -> description;
	}
	function  setDescription($description){
		$this -> description = $description;
	}
}

?>